# -*- coding: utf-8 -*-
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
from jianshu.items import JianshuItem

class JianshuSpiderSpider(CrawlSpider):
    name = 'jianshu_spider'
    allowed_domains = ['jianshu.com']
    start_urls = ['https://www.jianshu.com/p/52f8146cff61']

    rules = (
        Rule(LinkExtractor(allow=r'.*/p/[0-9a-z]{12}.*'), callback='parse_item', follow=True),
    )

    def parse_item(self, response):
        title = response.xpath('//h1[@class="title"]/text()').get()
        author = response.xpath('//div[@class="info"]/span/a/text()').get()
        pub_time = response.xpath("//span[@class='publish-time']/text()").get().replace('*','')
        content = response.xpath("//div[@class='show-content-free']").get()
        avatar = response.xpath('//div[@class="author"]/a/img/@src').get()
        article_id = response.url.split('?')[0].split('/')[-1]
        # print(title,author)
        read_count = response.xpath('//span[@class="views-count"]/text()').get().split(' ')[-1]
        comment_count = response.xpath('//span[@class="comments-count"]/text()').get().split(' ')[-1]
        like_count = response.xpath('//span[@class="likes-count"]/text()').get().split(' ')[-1]
        word_count = response.xpath('//span[@class="wordage"]/text()').get().split(' ')[-1]
        subjects = ','.join(response.xpath('//div[@class="include-collection"]//text()').getall())

        item = JianshuItem(
            title = title,
            avatar = avatar,
            author = author,
            article_id = article_id,
            pub_time = pub_time,
            origin_url = response.url,
            content = content,
            read_count = read_count,
            comment_count = comment_count,
            like_count = like_count,
            word_count = word_count,
            subjects = subjects
        )
        # print(item)
        yield item
